package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

public class ContactsFragment extends Fragment {

    private String[] people = {"段坤", "浪坤", "万坤", "刘坤", "周杰伦", "林俊杰"};
    private int[] imageIds = {
            R.drawable.duankun,
            R.drawable.langkun,
            R.drawable.wankun,
            R.drawable.liukun,
            R.drawable.zjl,
            R.drawable.ljj
    };
    private String[] phoneNumbers = {
            "电话号码：123-456-7890",
            "电话号码：234-567-8901",
            "电话号码：345-678-9012",
            "电话号码：456-789-0123",
            "电话号码：567-890-1234",
            "电话号码：678-901-2345"
    };
    private String[] addresses = {
            "家庭地址：广东汕头",
            "家庭地址：广东茂名",
            "家庭地址：湖北仙桃",
            "家庭地址：湖北黄冈",
            "家庭地址：中国台湾",
            "家庭地址：福建厦门"
    };

    // 新增数据字段
    private String[] ages = {
            "年龄：20岁",
            "年龄：20岁",
            "年龄：20岁",
            "年龄：19岁",
            "年龄：45岁",
            "年龄：43岁"
    };

    private String[] interests = {
            "兴趣爱好：音乐、跳舞",
            "兴趣爱好：音乐、篮球",
            "兴趣爱好：音乐、rap",
            "兴趣爱好：二次元",
            "兴趣爱好：音乐、摄影",
            "兴趣爱好：音乐、绘画"
    };

    private String[] jobs = {
            "工作：数学家",
            "工作：技术高手",
            "工作：音乐家",
            "工作：二次元领域大神",
            "工作：歌手",
            "工作：歌手"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contacts, container, false);

        ListView listView = view.findViewById(R.id.list_view);
        ContactsAdapter adapter = new ContactsAdapter();
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view1, position, id) -> {
            String selectedPerson = people[position];
            Intent intent = new Intent(getActivity(), ContactDetailActivity.class);
            intent.putExtra("person_name", selectedPerson);
            intent.putExtra("image_id", imageIds[position]);
            intent.putExtra("phone_number", phoneNumbers[position]);
            intent.putExtra("address", addresses[position]);
            intent.putExtra("age", ages[position]); // 添加年龄
            intent.putExtra("interest", interests[position]); // 添加兴趣爱好
            intent.putExtra("job", jobs[position]); // 添加工作
            startActivity(intent);
        });

        return view;
    }

    private class ContactsAdapter extends ArrayAdapter<String> {
        ContactsAdapter() {
            super(getActivity(), R.layout.list_item_contact, people);
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_contact, parent, false);
            }

            TextView nameTextView = convertView.findViewById(R.id.name_text_view);
            ImageView avatarImageView = convertView.findViewById(R.id.avatar_image_view);

            nameTextView.setText(people[position]);
            avatarImageView.setImageResource(imageIds[position]);

            return convertView;
        }
    }
}